class PkHandle {
    pk
    hasRight = false

    constructor(pk) {
        this.pk = pk
        this.hasRight = this._hasRight()
    }

    _hasRight() {
        return !!this.pk.right;
    }

    getLeftDetail() {
        const countDownTime = this._turnDuration(this.pk.left_duration)
        const rightRate = this._turnRate(this.pk.left_count)
        return {
            count: this.pk.left_count,
            score: this.pk.left_score,
            duration: countDownTime,
            rightRate
        }
    }

    getRightDetail() {
        if (this.pk.right_duration) {
            const countDownTime = this._turnDuration(this.pk.right_duration)
            const rightRate = this._turnRate(this.pk.right_count)
            return {
                count: this.pk.right_count,
                score: this.pk.right_score,
                duration: countDownTime,
                rightRate
            }
        }else {
            return {
                count: 0,
                score: 0,
                duration: '00:00',
                rightRate: '0%'
            }
        }
    }

    hasWinner() {
        return this.pk.winner;
    }

    getWinner() {
        if (this.pk.winner.id === this.pk.left.id) {
            return 'left'
        }else {
            return 'right'
        }
    }

    _turnDuration(duration) {
        let integerNum = parseInt(duration / 60)
        let decimalNum = duration % 60
        integerNum = integerNum.toString().length === 1 ? `0${integerNum}` : integerNum
        decimalNum = decimalNum.toString().length === 2 ? decimalNum : `0${decimalNum}`
        return `${integerNum}:${decimalNum}`
    }

    _turnRate(count) {
        if (this.pk.linked_questions.length == 0 || !this.pk.linked_questions) {
            return '0%'
        }else {
            let rate = (100 * (count / this.pk.linked_questions.length)).toFixed(1)
            return `${rate}%`
        }
    }
}
export {
    PkHandle
}
